package com.yyge.config;

import com.yyge.config.interceptor.AdminTokenInterceptor;
import com.yyge.config.interceptor.MobileTokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.Arrays;
/**
 * @Desc springmvc的核心配置文件
 * @Author: admin
 * @Date: 2023/06/29/17:12
 */
@Configuration
public class SpringMvcConfig implements WebMvcConfigurer {

//    @Autowired
//    @Qualifier("globalInterceptor")
//    private GlobalInterceptor globalInterceptor;

    /**
     * 自定义令牌有效性拦截器
     */
    @Autowired
    private AdminTokenInterceptor adminTokenInterceptor;

    /**
     * 自定义手机端的拦截器
     */
    @Autowired
    private MobileTokenInterceptor mobileTokenInterceptor;

    /**
     * 跨域
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
           .allowedOrigins("*")
           .allowedHeaders("*")
           .allowedMethods("*")
           .allowCredentials(false)
           .maxAge(1800);
    }

    /**
     * 添加自定义拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 多个拦截器的执行顺序addInterceptor顺序执行
        //使用拦截器
        //放行的url
        List<String> overAll=new ArrayList<>();
        overAll.add("/page/login/**");
        overAll.add("/swagger-resources/**");
        overAll.add("/swagger-ui/**");
        overAll.add("/v3/**");
        overAll.add("/front/**");
        overAll.add("/api/**");
        overAll.add("/mobile/**");
        overAll.add("/upload/**");
        registry.addInterceptor(adminTokenInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(overAll);


        //添加令牌拦截器
        List<String> excludeUrls=new ArrayList<>();
        excludeUrls.add("/api/captcha");
        excludeUrls.add("/api/front/**");
        excludeUrls.add("/api/login/**");
        registry.addInterceptor(adminTokenInterceptor)
                .addPathPatterns("/api/**")
                .excludePathPatterns(excludeUrls);

        System.out.println("放行拦截器？？？");
        //放行的url手机端
        List<String> excludeMobileUrls=new ArrayList<>();
        excludeMobileUrls.add("/mobile/login");

        //添加令牌拦截器
        registry.addInterceptor(mobileTokenInterceptor).addPathPatterns("/mobile/**").excludePathPatterns(excludeMobileUrls);
        /**
         * Swageer放行通道
         * /swagger-resources/**
         * /swagger-ui/**
         * /v3/**
         */

        //http://localhost:8080/api/swagger-ui/index.html
    }

     /**
     * 跨域配置
     */
    @Bean
    public CorsFilter corsFilter()
    {
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        // 设置访问源地址
        config.addAllowedOriginPattern("*");
        // 设置访问源请求头
        config.addAllowedHeader("*");
        // 设置访问源请求方法
        config.addAllowedMethod("*");
        // 有效期 1800秒
        config.setMaxAge(1800L);

        // 添加映射路径，拦截一切请求
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);
        // 返回新的CorsFilter
        return new CorsFilter(source);
    }

    /**
        静态资源放行
        @param registry
    */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
          //放行Swagger3
         registry.addResourceHandler("swagger-ui.html")
         //registry.addResourceHandler("*")
         .addResourceLocations("classpath:/META-INF/resources/");
    }


    /**
        静态资源放行
        @paramregistry
     */
//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry){
//      registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
//      registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
//    }
}
